<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\package;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
class PackageController extends CommonController
{
    //首页
    public function index()
    {
        $data = package::all();
        //dd($data);
        return view("admin.package.index")->with("data",$data);
    }
    //添加post
    public function store()
    {
        $input = Input::except("_token");
        //dd($input);
        //验证规则
        $rules =[
            'tao_name' => "required",
            'tao_content' => "required",
        ];
        //错误信息
        $message = [
            'tao_name.required' => "请填写套餐名称！",
            'tao_content.required' => "请填写套餐信息！",
        ];
        //验证
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){//验证成功
            $package = new package;//实例化模型
            $re = $package->add($input);//调用add方法
            //echo $re;//{"tao_name":"111","tao_content":"111","tid":4}
            if($re){
                return redirect("admin/package");
            }else{
                return back()->with("errors","添加失败，稍后再试！");
            }
        }else{//验证失败
            return back()->withErrors($validator);
        }

//        if(){
//
//        }else{
//
//        }
    }
    //新增页面
    public function create()
    {
        return view("admin.package.addPackage");
    }
    //
    public function show()
    {

    }
    //更新数据
    public function update($pid)
    {
        $input = Input::except("_token","_method");
        $data = package::where("tid",$pid)->update($input);
        if($data){
            return redirect("admin/package");
        }else{
            return back()->with("errors","编辑失败，请重试！");
        }
    }
    //编辑yemian
    public function edit($pid)
    {
        $data = package::find($pid);
        return view("admin.package.editPackage",compact("data"));
    }
    //删除
    public function destroy($tid)
    {
        $re = package::where("tid",$tid)->delete();
        if($re){
            $data = [
                "status"=>0,
                "message"=>"删除成功!",
            ];
        }else{
            $data = [
                "status"=>1,
                "message"=>"删除失败!",
            ];
        }
        return $data;
    }

}
